package com.originskyseed.platform.user.service.impl;

import java.math.BigDecimal;
import java.util.*;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.ParameterConfigureMapper;
import com.originskyseed.platform.user.mapper.PlayerMapper;
import com.originskyseed.platform.user.mapper.PlayerRelateRechargeMapper;
import com.originskyseed.platform.user.mapper.UserCumulativeChargeMapper;
import com.originskyseed.platform.user.mapper.UserInfoMapper;
import com.originskyseed.platform.user.model.ParameterConfigure;
import com.originskyseed.platform.user.model.Player;
import com.originskyseed.platform.user.model.PlayerRelateRecharge;
import com.originskyseed.platform.user.model.UserCumulativeCharge;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IPlayerRelateRechargeService;

/**
 * 玩家充值信息表Service接口实现类（OSI_PLAYER_RELATE_RECHARGE）
 *
 * @author wlj
 */
@DubboService
@Component("playerRelateRechargeService")
public class PlayerRelateRechargeServiceImpl implements IPlayerRelateRechargeService {

	@Resource
	private PlayerRelateRechargeMapper playerRelateRechargeMapper;

	@Resource
	private PlayerMapper playerMapper;

	@Resource
	private UserInfoMapper userInfoMapper;

	@Resource
	private ParameterConfigureMapper parameterConfigureMapper;

	@Resource
	private UserCumulativeChargeMapper userCumulativeChargeMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.playerRelateRechargeMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.playerRelateRechargeMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(PlayerRelateRecharge record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.playerRelateRechargeMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(PlayerRelateRecharge record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.playerRelateRechargeMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(PlayerRelateRecharge record) throws Exception {
		record.setUpdateTime(new Date());
		return this.playerRelateRechargeMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(PlayerRelateRecharge record) throws Exception {
		record.setUpdateTime(new Date());
		return this.playerRelateRechargeMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public PlayerRelateRecharge selectByPrimaryKey(String id) throws Exception {
		return this.playerRelateRechargeMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PlayerRelateRecharge> selectList(PlayerRelateRecharge record) throws Exception {
		return this.playerRelateRechargeMapper.selectList(record);
	}


	public BasePage<PlayerRelateRecharge> selectListByPageByTenantCode(String tenantCode,Map<String, Object> params, BasePage<PlayerRelateRecharge> page) throws Exception
	{
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		params.put("page", page);
		List<PlayerRelateRecharge> list = this.playerRelateRechargeMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PlayerRelateRecharge> selectListByPage(Map<String, Object> params, BasePage<PlayerRelateRecharge> page) throws Exception {
		params.put("page", page);
		List<PlayerRelateRecharge> list = this.playerRelateRechargeMapper.selectListByPage(params);
		if (list != null && list.size() > 0) {
			for (PlayerRelateRecharge playerRelateRecharge : list) {
				String channelSubId=playerRelateRecharge.getChannelSubId();
				if(channelSubId!=null&&StringUtils.isNotBlank(channelSubId)) {
					UserInfo userInfo = this.userInfoMapper.selectByOtherAccount(channelSubId);
					if (userInfo != null) {
						playerRelateRecharge.setBelongBusinessId(userInfo.getId());
						playerRelateRecharge.setBelongBusinessName(userInfo.getName());
						playerRelateRecharge.setBelongBusinessAccount(userInfo.getAccount());
					}
				}
				Calendar cal1 = Calendar.getInstance();
				cal1.setTime(new Date());
				Date registerTime = playerRelateRecharge.getRegisterTime();
				if (registerTime != null) {
					Calendar cal2 = Calendar.getInstance();
					cal2.setTime(registerTime);
					int monDif = cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH);
					int month = (cal1.get(Calendar.YEAR)-cal2.get(Calendar.YEAR))*12;
					playerRelateRecharge.setMonth(month + monDif + "");
				}
			}
		}
		page.setResults(list);
		return page;
	}

	/**
	 * 推广平台_业务员_查看玩家充值记录分页查询列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PlayerRelateRecharge> selectPlayerRelateRechargeListToSalesman(Map<String, Object> params, BasePage<PlayerRelateRecharge> page) throws Exception {
		params.put("page", page);
		List<PlayerRelateRecharge> list = this.playerRelateRechargeMapper.selectPlayerRelateRechargeListToSalesman(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 推广平台_业务员_查看玩家充值记录Excel导出
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PlayerRelateRecharge> PlayerRelateRechargeListToSalesman(Map<String, Object> params) throws Exception {
		BasePage<PlayerRelateRecharge> page = new BasePage();
		page.setPageNo(1);
		page.setPageSize(999999);
		params.put("page", page);
		return this.playerRelateRechargeMapper.selectPlayerRelateRechargeListToSalesman(params);
	}

	/**
	 * 推广平台_结算报表统计
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PlayerRelateRecharge> finacialSettlementToMonth(Map<String, Object> params, BasePage<PlayerRelateRecharge> page) throws Exception {
		params.put("page", page);
		List<PlayerRelateRecharge> list = this.playerRelateRechargeMapper.finacialSettlementToMonth(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 推广平台_结算报表统计_导出结算函
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PlayerRelateRecharge> finacialSettlementLetter(Map<String, Object> params) throws Exception {
		return this.playerRelateRechargeMapper.finacialSettlementToMonth(params);
	}

	/**
	 * 推广平台_收入对账表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PlayerRelateRecharge> selectRevenueReconcliationList(Map<String, Object> params, BasePage<PlayerRelateRecharge> page) throws Exception {
		params.put("page", page);
		List<PlayerRelateRecharge> list = this.playerRelateRechargeMapper.selectRevenueReconcliationList(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 推广平台_收入对账表导出Excel
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PlayerRelateRecharge> selectRevenueReconcliationListToExcel(Map<String, Object> params) throws Exception {
		return this.playerRelateRechargeMapper.selectRevenueReconcliationList(params);
	}

	/**
	 * 批量插入记录
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public int insertBatch(List<PlayerRelateRecharge> list) throws Exception {
		return this.playerRelateRechargeMapper.insertBatch(list);
	}


	/**
	 * 批量插入记录
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public int insertBatch(String tenantCode,List<PlayerRelateRecharge> list) throws Exception
	{
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return  insertBatch(list);
	}

	/**
	 * 批量插入记录,月表
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public int insertMonthBatch(List<PlayerRelateRecharge> list) throws Exception {
		return this.playerRelateRechargeMapper.insertMonthBatch(list);
	}


	/**
	 * 批量插入记录,月表
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public int insertMonthBatch(String tenantCode,List<PlayerRelateRecharge> list) throws Exception
	{
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return insertMonthBatch(list);
	}

	/**
	 * 用户累计充值金额
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public BigDecimal totalAmount(String id) throws Exception {
		return this.playerRelateRechargeMapper.totalAmount(id);
	}

	/**
	 * 充值列表统计充值金额
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public BigDecimal selectTotalAmountByMap(Map<String, Object> params) throws Exception {
		return this.playerRelateRechargeMapper.selectTotalAmountByMap(params);
	}

	/**
	 * 分页查询支付明细列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PlayerRelateRecharge> selectPaymentDetailsByPage(Map<String, Object> params, BasePage<PlayerRelateRecharge> page) throws Exception {
		params.put("page", page);
		List<PlayerRelateRecharge> list = this.playerRelateRechargeMapper.selectPaymentDetailsByPage(params);

		Map<String,Object> map = new HashMap<>();
		for (PlayerRelateRecharge playerRelateRecharge:list) {
			Calendar cal1 = Calendar.getInstance();
			cal1.setTime(new Date());
			Date registerTime = playerRelateRecharge.getRegisterTime();
			if (registerTime != null) {
				Calendar cal2 = Calendar.getInstance();
				cal2.setTime(registerTime);
				int monDif = cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH);
				int month = (cal1.get(Calendar.YEAR)-cal2.get(Calendar.YEAR))*12;
				playerRelateRecharge.setMonth(month + monDif + "");
			}
			String channelSubId=playerRelateRecharge.getChannelSubId();
			if(channelSubId!=null&&StringUtils.isNotBlank(channelSubId)) {
				UserInfo userInfo=userInfoMapper.selectByOtherAccount(channelSubId);
				playerRelateRecharge.setBelongBusinessName(userInfo==null?"":userInfo.getName());
			}
		}
		page.setResults(list);
		return page;
	}

	/**
	 * 查询支付明细列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public List<PlayerRelateRecharge> selectPaymentDetailsByMap(Map<String, Object> params) throws Exception{
		return this.playerRelateRechargeMapper.selectPaymentDetailsByMap(params);
	}

	/**
	 * 查询orderId是否存在
	 * 
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public List<String> selectOrderIds( String tenantCode,List<String> list) throws Exception {
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.playerRelateRechargeMapper.selectOrderIds(list);
	}


	/**
	 * 查询orderId是否存在
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public List<String> selectOrderIds(List<String> list) throws Exception
	{
		return this.playerRelateRechargeMapper.selectOrderIds(list);
	}

	/**
	 * 统计业务员累充类型
	 * 
	 * @param tenantCode
	 * @return
	 * @throws Exception
	 */
	public int insertUserCumulativeRechargeCount(String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		ParameterConfigure queryParam = this.parameterConfigureMapper.selectByCode("CUMULATIVE_RECHARGE_TYPE");
		if (queryParam != null) {
			List<ParameterConfigure> parameterConfigureList = this.parameterConfigureMapper.selectListByParentId(queryParam.getId());
			if (parameterConfigureList.size() > 0) {
				UserInfo query = new UserInfo();
				query.setType(6);
				query.setSourceSys(2);
				List<UserInfo> userInfoList = this.userInfoMapper.selectList(query);
				for (UserInfo userInfo : userInfoList) {
					PlayerRelateRecharge queryPlayerRel = new PlayerRelateRecharge();
					queryPlayerRel.setChannelSubId(userInfo.getOtherAccount());
					List<PlayerRelateRecharge> playerRelateRechargeList = this.playerRelateRechargeMapper.selectList(queryPlayerRel);
					for (ParameterConfigure parameterConfigure : parameterConfigureList) {
						BigDecimal amount = BigDecimal.ZERO;
						for (PlayerRelateRecharge playerRelateRecharge : playerRelateRechargeList) {
							amount = amount.add(playerRelateRecharge.getAmount());
							if (amount.compareTo(new BigDecimal(parameterConfigure.getValue())) > 0) {
								UserCumulativeCharge userCumulativeCharge = new UserCumulativeCharge();
								userCumulativeCharge.setUserId(userInfo.getId());
								userCumulativeCharge.setType(parameterConfigure.getName());
								List<UserCumulativeCharge> userCumulativeChargeList = this.userCumulativeChargeMapper.selectList(userCumulativeCharge);
								if (userCumulativeChargeList.size() == 0) {
									UserCumulativeCharge userCumulativeChargeAdd = new UserCumulativeCharge();
									userCumulativeChargeAdd.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
									userCumulativeChargeAdd.setType(parameterConfigure.getName());
									userCumulativeChargeAdd.setUserId(userInfo.getId());
									userCumulativeChargeAdd.setName(userInfo.getName());
									userCumulativeChargeAdd.setAccount(userInfo.getAccount());
									userCumulativeChargeAdd.setCompletionTime(playerRelateRecharge.getRechargeTime());
									userCumulativeChargeAdd.setAmount(amount);
									userCumulativeChargeAdd.setCreateTime(new Date());
									this.userCumulativeChargeMapper.insertSelective(userCumulativeChargeAdd);
								}
								break;
							}
						}
					}
				}
			}
		}
		return 1;
	}

	/**
	 * 查询内容平台导出
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public List<PlayerRelateRecharge> selectContentExcel(Map<String, Object> params) throws Exception {
		return this.playerRelateRechargeMapper.selectContentExcel(params);
	}


	/**
	 * 查询累充金额，根据玩家id和时间
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public BigDecimal selectTotalAmountByPlayerIdAndTime(Map<String, Object> params) throws Exception {
		return this.playerRelateRechargeMapper.selectTotalAmountByPlayerIdAndTime(params);
	}

	/**
	 * 查询上一次的累充金额
	 *
	 * @param playerId
	 * @return
	 * @throws Exception
	 */
	public BigDecimal selectAmountByPlayerId(String tenantCode,String playerId) throws Exception{
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.playerRelateRechargeMapper.selectAmountByPlayerId(playerId);
	}

	/**
	 * 查询上一次的累充金额
	 *
	 * @param playerId
	 * @return
	 * @throws Exception
	 */
	public BigDecimal selectAmountByPlayerId(String playerId) throws Exception
	{
		return this.playerRelateRechargeMapper.selectAmountByPlayerId(playerId);
	}
}
